package tools.code.generator.service;

import tools.code.generator.model.FieldModel;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class FieldInfo implements ExcelOutApi {
    public String simpleName;
    public String name;
    public String type;
    public String comment;
    public String classify;
    public String group;

    public String supportflag;

    public FieldInfo() {

    }


    public String getHiveType() {
        String toType;
        if (StringUtils.equals(type, "long")) {
            toType = "bigint";
        } else if (StringUtils.equals(type, "int")) {
            toType = "int";
        } else if (StringUtils.equals(type, "double")) {
            toType = "double";
        } else {
            toType = "string";
        }
        return toType;
    }

    public String getParquetType() {
        String toType;
        if (StringUtils.equals("int", type)) {
            toType = "Integer";
        } else if (StringUtils.equals("long", type)) {
            toType = "Long";
        } else if (StringUtils.equals("double", type)) {
            toType = "Double";
        } else {
            toType = "String";
        }
        return toType;
    }

    public String getGetMethodName() {
        return String.format("get%s", StringUtils.capitalize(name));
    }

    public String getSetMethodName() {
        return String.format("set%s", StringUtils.capitalize(name));
    }

    public String getPojoFieldType() {
        String toType;
        if (StringUtils.equals("int", type)) {
            toType = "Integer";
        } else if (StringUtils.equals("long", type)) {
            toType = "Long";
        } else if (StringUtils.equals("double", type)) {
            toType = "Double";
        } else {
            toType = type;
        }
        return toType;
    }

    public boolean isConvertToJsonStr() {
        if (StringUtils.equalsIgnoreCase("int", this.type) || StringUtils.equalsIgnoreCase("int", this.type) || StringUtils.equalsIgnoreCase("long", this.type) || StringUtils.equalsIgnoreCase("string", this.type) || StringUtils.equalsIgnoreCase("double", this.type)) {
            return false;
        }
        return true;
    }

    public String getHiveName() {
        return strCamelToSnakeCase(this.name);
    }

    /**
     * 将驼峰字符串转为下划线分隔的形式，如terminalId => terminal_id
     * @param name
     * @return
     */
    private  String strCamelToSnakeCase(String name) {
        if (name == null || "".equalsIgnoreCase(name)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            if (Character.isUpperCase(ch)) {
                sb.append("_").append(Character.toLowerCase(ch));
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    /**
     * 0200协议是否支持
     */
    public boolean isSupport0200() {
        return "true".equalsIgnoreCase(supportflag);
    }

    @Override
    public List<FieldModel> excelOutList() {
        List<FieldModel> cols = new ArrayList<>();
        cols.add(new FieldModel("name", name, "字段名"));
        cols.add(new FieldModel("type", type, "字段类型"));
        cols.add(new FieldModel("simpleName", simpleName, "Mongo字段名"));
        cols.add(new FieldModel("hiveTableName", getHiveName(), "Hive字段名"));
        cols.add(new FieldModel("hiveType", getHiveType(), "Hive字段类型"));
        cols.add(new FieldModel("supportflag", isSupport0200() ? "是" : "否", "是否支持0200"));
        cols.add(new FieldModel("comment", comment, "说明"));
        return cols;
    }
}